const app = new getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    prompt: '',
    default_img: '',
    member_info: {
      level_name: '未登录...'
    },
    integralConfig: {}, //积分赠送配置
    isSign: 0, //是否签到
    unpaidOrder: 0, //待支付
    shipmentPendingOrder: 0, //待发货
    goodsNotReceivedOrder: 0, //待收货
    refundOrder: 0, //退款
    verification: 0, //是否本店核销员
    is_login: 0,
    mask_status: 0,
    member_account: {
      point: 0,
      balance: 0
    },
    coupon_count: 0,
    member_level: {},
    order_num_list: {
      wait_pay: 0,
      wait_delivery: 0,
      wait_recieved: 0,
      recieved: 0,
      refunding: 0,
      pintuan_order_num: 0,
      presell_order_num: 0
    },
    promoter_info: ''
  },

  /**
   * 生命周期函数--监听页面加载inputNickName                
   */
  onLoad: function(options) {


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this;

    let tab_parm = app.globalData.tab_parm;
    let tab_type = app.globalData.tab_type;

    if (tab_parm == 'cancle_pay') {
      let url = '/pagesother/pages/order/myorderlist/myorderlist?status=0';

      app.setTabParam('');
      app.setTabType('');
      wx.navigateTo({
        url: url,
      })
    } else {
      //检测登录状态

      let is_login = app.globalData.is_login;
      that.setData({
        is_login: is_login
      })
      if (is_login == 1) {
        app.defaultImg(that, true);
      }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 获取用户信息
   */
  getWechatUserInfo: function(res) {
    let that = this;
    // 获取用户授权
    if (res.detail.errMsg == 'getUserInfo:fail auth deny') {
      app.showModal({
        content: '你已取消授权，需授权后才能登录！',
      })
      return false;
    } else if (res.detail.errMsg == 'getUserInfo:ok') {
      let session_key = app.globalData.session_key;
      if (app.checkEmpty(session_key, '') == '') {
        app.baseLogin();
      } else {
        app.login(res.detail.encryptedData, res.detail.iv);
      }
      //参数检测
      app.getUserSetting();
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      if (this.userInfoReadyCallback) {
        this.userInfoReadyCallback(res)
      }

      //检测是否开启强制会员绑定
      let is_first_bind = app.globalData.is_first_bind;
      if (is_first_bind == 1) {
        wx.reLaunch({
          url: '/pagesother/pages/login/login/login',
        })
        return;
      }
      //检测是否登录/注册成功
      let is_login = app.globalData.is_login;

      if (is_login == 1) {
        that.setData({
          is_login: is_login
        })
        app.defaultImg(that, true);
        app.isNotLogin(1);
        app.setSourceId('');
      } else {
        let times = 0;
        setTimeout(function() {
          let load_timer = setInterval(function() {
            times++;
            //检测是否开启强制会员绑定
            let is_first_bind = app.globalData.is_first_bind;
            if (is_first_bind == 1) {
              wx.reLaunch({
                url: '/pagesother/pages/login/login/login',
              })
              clearInterval(load_timer);
              return;
            }
            //检测是否登录/注册成功
            is_login = app.globalData.is_login;

            if (is_login == 1) {
              //登录成功调用是否是之前从其页面跳转过来
              if (!app.isNotLogin(1)) {
                app.defaultImg(that, true);
                app.showBox(that, '登陆成功');

                that.setData({
                  is_login: 1,
                  mask_status: 0
                })
                app.setSourceId('');
                clearInterval(load_timer);
                return;
              }

            } else if (times == 30) {
              wx.showModal({
                title: '提示',
                content: '登录超时...',
                showCancel: false,
                success: function(res) {
                  wx.reLaunch({
                    url: '/pagesother/pages/login/login/login',
                  })
                }
              })
              clearInterval(load_timer);
            }
          }, 500);
        }, 500);
      }
    } else {
      app.showModal({
        content: '授权失败!',
        url: '/pages/index/index'
      })
    }
  },

  /**
   * 加载会员信息
   */
  loadInfo: function() {
    let that = this;
    //会员详情
    app.sendRequest({
      url: 'System.Member.memberInfo',
      data: {},
      success: function(res) {
        let code = res.code;
        let data = res.data;
        if (code == 0) {
          let member_info = data;
          let img = member_info.user_info.user_headimg;
          member_info.user_info.user_headimg = app.IMG(img); //图片路径处理

          that.setData({
            member_info: res.data,
          })

          that.loadMemberLevel();
        }
      }
    })
    app.addonIsExit(that, true, 'loadPromoterInfo');

    //会员账户信息
    app.sendRequest({
      url: 'System.Member.memberAccount',
      data: {},
      success: function(res) {
        let code = res.code;
        let data = res.data;
        if (code == 0) {
          let member_account = data;
          that.setData({
            member_account: member_account,
          })
        }
      }
    })

    //会员优惠券
    app.sendRequest({
      url: 'System.Member.couponNum',
      data: {},
      success: function(res) {
        let code = res.code;
        let data = res.data;
        if (code == 0) {
          let coupon_count = data;
          that.setData({
            coupon_count: coupon_count,
          })
        }
      }
    })

    //会员订单信息
    app.sendRequest({
      url: 'System.Member.memberOrderCount',
      data: {},
      success: function(res) {
        let code = res.code;
        let data = res.data;
        if (code == 0) {
          let order_num_list = data;
          that.setData({
            order_num_list: order_num_list,
          })
        }
      }
    })

    //核销员信息
    app.sendRequest({
      url: 'System.Order.checkVerification',
      data: {},
      success: function(res) {
        if (res.code == 0) {
          let verification = app.checkEmpty(res.data, 0);
          that.setData({
            verification: verification,
          })
        }
      }
    })
  },

  //获取会员等级
  loadMemberLevel: function() {
    let that = this;
    app.sendRequest({
      url: 'System.Member.memberLevel',
      data: {
        level_id: that.data.member_info.member_level
      },
      success: function(res) {
        let code = res.code;
        let data = res.data;
        if (code == 0) {
          that.setData({
            member_level: res.data,
          })
        }
      }
    })
  },

  /**
   * 加载推广员信息
   */
  loadPromoterInfo: function() {
    let that = this;

    // 推广员信息
    app.sendRequest({
      url: 'Nsfx.Distribution.promoterDetail',
      data: {},
      success: function(res) {

        if (res.code == 0) {
          let promoter_info = app.checkEmpty(res.data, '');
          that.setData({
            promoter_info: promoter_info
          })
        }
      }
    });

    // 佣金信息
    app.sendRequest({
      url: 'System.Distribution.myCommission',
      data: {},
      success: function(res) {

        if (res.code == 0) {
          let user_account = app.checkEmpty(res.data, {});
          that.setData({
            user_account: user_account
          })
        }
      }
    });

    // 店铺配置
    app.sendRequest({
      url: 'System.Distribution.shopConfig',
      data: {},
      success: function(res) {

        if (res.code == 0) {
          let shop_config = app.checkEmpty(res.data, {});
          that.setData({
            shop_config: shop_config
          })
        }
      }
    });
  },

  /**
   * 头像加载失败
   */
  errorHeadImg: function(e) {
    let that = this;
    let member_info = that.data.member_info;
    let default_img = that.data.default_img;
    let img = member_info.user_info.user_headimg;
    let parm = {};
    let parm_key = "member_info.user_info.user_headimg";

    if (default_img.is_use == 1) {
      default_img = default_img.value.default_headimg;
      if (img.indexOf(default_img) == -1) {
        parm[parm_key] = default_img;
        that.setData(parm);
      }
    }
  },
})